<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html   xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台管理 - 易买网</title>
<link type="text/css" rel="stylesheet" href="../css/style.css" />
<script type="text/javascript" src="../scripts/function-manage.js"></script>
    <script type="text/javascript" src="../scripts/jquery-1.8.3.js"></script>
</head>
<body>
<div class="main">
	<h2>&nbsp;&nbsp;新闻管理&nbsp;<input type="text" size="30" placeholder="请输入关键字" id="keyword" ><input type="button" value="搜索" sousuo="cuxiao" id="keywords"/></h2>
	<div class="manage">
		<table class="list" id="tbnews">
			<tr>
				<td colspan="2" width="50%" class="bankuai" id="cuxiao" style="background-color: #fc7e31">促销信息</td>
				<td colspan="2" width="50%" class="bankuai" id="gonggao" style="background-color: #fc7e31">公告新闻</td>
			</tr>
			<tr>
				<th><input type="checkbox" id="quanxuan"/><label for="quanxuan">全选&nbsp;&nbsp;ID</label></th></label>
				<th>新闻标题</th>
				<th>发布时间</th>
				<th>操作</th>
			</tr>

		</table>
		<a href="javascript:void (0)" id="firstpage" class="pages" fenye='CuXiaoFenYe'>首页</a>
		<a href="javascript:void (0)" id="prevpage" class="pages"  fenye='CuXiaoFenYe'>上一页</a>
		<a href="javascript:void (0)" id="nextpage" class="pages" fenye='CuXiaoFenYe'>下一页</a>
		<a href="javascript:void (0)" id="lastpage" class="pages" fenye='CuXiaoFenYe'>尾页</a>
		<span style='float:right'><a href='javascript:void (0)' id='quanbushanchu'>删除已选</a></span>
	</div>
</div>
</body>
</html>
<script type="text/javascript">
    var pageIndex;//当前页
    var totalPage;//总页数

    $(function () {



        //异步删除多条信息
		var  delNewsAjax = function (idstr) {
			$.ajax({
				type:'POST',
				url:'../DelNewsServlet',
				data:{
                    idStr: idstr,
					state: 1
				},
				success:function(data){
				    if(data){
				        getCuXiaoNews(1);
                        alert("删除成功");
					}else{
                        getCuXiaoNews(1);
						alert("删除失败");
					}
				}

			})
        }




		//促销信息分页
		function cuxiaofenye(){
            $("[fenye='CuXiaoFenYe']").unbind()
            $("[fenye='CuXiaoFenYe']").on("click",function () {
                var pageflag = $(this).attr("id")

                if (pageflag=="firstpage"){
                    getCuXiaoNews(1);
                } else if (pageflag=="prevpage"){
                    pageIndex--;
                    if(pageIndex<1){
                        pageIndex=1;
                    }
                    getCuXiaoNews(pageIndex);
                } else if (pageflag == "nextpage"){
                    pageIndex++;
                    if(pageIndex>totalPage){
                        pageIndex=totalPage;
                    }
                    getCuXiaoNews(pageIndex);
                } else if(pageflag=="lastpage"){
                    getCuXiaoNews(totalPage);
                }
            })
		}

        //页面加载时加载促销信息
        getCuXiaoNews();
        cuxiaofenye();
        cuxiaosousuo();

		//公告信息分页
		function gonggao(){
            $("[fenye='GongGaoFenYe']").unbind();
            $("[fenye='GongGaoFenYe']").on("click",function () {
                var pageflag = $(this).attr("id")

                if (pageflag=="firstpage"){
                    getGongGaoNews(1);
                } else if (pageflag=="prevpage"){
                    pageIndex--;
                    if(pageIndex<1){
                        pageIndex=1;
                    }
                    getGongGaoNews(pageIndex);
                } else if (pageflag == "nextpage"){
                    pageIndex++;
                    if(pageIndex>totalPage){
                        pageIndex=totalPage;
                    }
                    getGongGaoNews(pageIndex);
                } else if(pageflag=="lastpage"){
                    getGongGaoNews(totalPage);
                }
            })
		}

		//板块选择变化
		$("#cuxiao").click(function(){
		    $(".bankuai").css("background-color","#fc7e31");
		    $("#cuxiao").css("background-color","white");

            $(".pages").attr("fenye","CuXiaoFenYe");
            $("#keywords").attr("sousuo","cuxiao");
            cuxiaosousuo();
            getCuXiaoNews(1);
            cuxiaofenye();

		})


        $("#gonggao").click(function(){
            $(".bankuai").css("background-color","#fc7e31");
            $("#gonggao").css("background-color","white");
          /*  $(".pages").removeAttribute("fenye")*/
            $(".pages").attr("fenye","GongGaoFenYe");
            $("#keywords").attr("sousuo","gonggao");
            gonggaosousuo();
            getGongGaoNews(1);
            gonggao();
        })

		//全选功能


        $("#quanxuan").click(function(){
            if($(this).is(":checked")){
                $("input[name='enid']").prop("checked",true);
			}else{
                $("input[name='enid']").prop("checked",false);
			}
		})

        //删除勾选
		$("#quanbushanchu").click(function(){
		    var endids = new Array();
		    $("input:checkbox[name='enid']:checked").each(function(){
		        endids.push($(this).val());
			})
			var endidstr = endids.join(',');
            if(confirm("确认删除?(删除后数据清除)")){
                delNewsAjax(endidstr);
            }

		})



    })

    //异步分页获取促销信息
    var getCuXiaoNews = function(curr){
        $.ajax({
            type:'POST',
            url:'../CuXiaoNewsJsonServlet',
            dataType:'JSON',
            data: {
                keyword: $("#keyword").val(),
                pageIndex: curr || 1
            },
            success:function(data){
                $("#tbnews").find(".contextdata").remove();
                $("#tbnews").find(".page").remove();
                $.each(data.newsList,function (index,obj) {
                    var $newsStr="<tr class='contextdata'><td class='first w4 c'><div style='text-align:left '><input type='checkbox' id='enid"+obj.enid+"' name='enid' value='"+obj.enid+"'></div>&nbsp;&nbsp;&nbsp;<label for='enid"+obj.enid+"'>"+obj.enid+"</label></td><td  style='text-align:center'><a href='../ShowNewsServlet?enid="+obj.enid+"'>"+obj.entitle+"</a></td><td>"+obj.encreatetime+"</td><td class='w1 c'><a href='../ToUpdateNewsServlet?enid="+obj.enid+"'>修改</a>&nbsp;&nbsp;&nbsp;<a href='javascript:querenshanchu("+obj.enid+")'>删除</a></td></tr>";
                    $("#tbnews").append($newsStr);
                })
                var $pageinfor="<tr class='page'><td colspan=4>共有<span id='total_page'>"+data.totalPages+"<span>页&nbsp;"+data.totalRows+"条记录&nbsp;当前为第<span id='curr_page'>"+data.pageIndex+"</span>页</td></tr>"
                $("#tbnews").append($pageinfor);

                totalPage = data.totalPages;
                pageIndex = data.pageIndex;
            }
        })
    }



    //异步分页获取公告信息
    var getGongGaoNews = function(curr){
        $.ajax({
            type:'POST',
            url:'../GongGaoNewsJsonServlet',
            dataType:'JSON',
            data: {
                keyword: $("#keyword").val(),
                pageIndex: curr || 1
            },
            success:function(data){
                $("#tbnews").find(".contextdata").remove();
                $("#tbnews").find(".page").remove();
                $.each(data.newsList,function (index,obj) {
                    var $newsStr="<tr class='contextdata'><td class='first w4 c'><div style='text-align:left '><input type='checkbox' id='enid"+obj.enid+"' name='enid' value='"+obj.enid+"'></div>&nbsp;&nbsp;&nbsp<label for='enid"+obj.enid+"'>"+obj.enid+"</label></td><td  style='text-align:center'><a href='../ShowNewsServlet?enid="+obj.enid+"'>"+obj.entitle+"</a></td><td>"+obj.encreatetime+"</td><td class='w1 c'><a href='../ToUpdateNewsServlet?enid="+obj.enid+"'>修改</a>&nbsp;&nbsp;&nbsp;<a href='javascript:querenshanchu("+obj.enid+")'>删除</a></td></tr>";
                    $("#tbnews").append($newsStr);
                })
                var $pageinfor="<tr class='page'><td colspan=4>共有<span id='total_page'>"+data.totalPages+"<span>页&nbsp;"+data.totalRows+"条记录&nbsp;当前为第<span id='curr_page'>"+data.pageIndex+"</span>页</td></tr> "
                $("#tbnews").append($pageinfor);

                totalPage = data.totalPages;
                pageIndex = data.pageIndex;
            }
        })
    }

    //异步删除单条信息
    var  delSingleNewsAjax = function (enid) {
        $.ajax({
            type:'POST',
            url:'../DelNewsServlet',
            data:{
                enid: enid
            },
            success:function(data){
                if(data){
                    getCuXiaoNews(1);
                    alert("删除成功");
                }else{
                    getCuXiaoNews(1);
                    alert("删除失败");
                }
            }

        })
    }
    //确认删除
	function querenshanchu(enid){
        if(confirm("确认删除?(删除后数据清除)")){
            delSingleNewsAjax(enid)
		}
	}

	//促销关键词搜索
    function cuxiaosousuo(){
        $("[sousuo='cuxiao']").unbind()
        $("[sousuo='cuxiao']").on("click",function () {
            getCuXiaoNews(1);
        })
    }
    //公告关键字搜索
    function gonggaosousuo(){
        $("[sousuo='gonggao']").unbind()
        $("[sousuo='gonggao']").on("click",function () {
            getGongGaoNews(1);
        })
    }


</script>